<?php


namespace app\bidco\controller;

use think\Db;


class Lot extends \Common {

    # 获取正在进行或者没有进行的专场列表
    public function GetLots(){
        if(isset($_GET["limit"]) && isset($_GET["page"]) && isset($_GET["name"])){
            $name = $_GET["name"];
            $limit = $_GET["limit"];
            $page = $_GET["page"];

            if($name == ""){
                $lots = Db::name( "lot")
                    ->where("is_over", 0)
                    ->where("is_del", 0)
                    ->where("state = 0 or state = 1")
                    ->where("name like '%$name%'")
                    ->order("state desc,lid desc")
                    ->page($page, $limit)
                    ->select();
                $count = Db::name( "lot")
                    ->where("is_over", 0)
                    ->where("is_del", 0)
                    ->where("state = 0 or state = 1")
                    ->where("name like '%$name%'")
                    ->count();
            }else{
                $lots = Db::name( "lot")
                    ->where("is_over", 0)
                    ->where("is_del", 0)
                    ->where("state = 0 or state = 1")
                    ->where("name like '%$name%'")
                    ->order("state desc,lid desc")
                    ->page($page, $limit)
                    ->select();
                $count = Db::name( "lot")
                    ->where("is_over", 0)
                    ->where("is_del", 0)
                    ->where("state = 0 or state = 1")
                    ->where("name like '%$name%'")
                    ->count();
            }


            $data = [];
            if(!empty($lots)){
                foreach ($lots as $val){
                    $GoodsCount = Db::name( "goods")->where("lid", $val["lid"])->count();
                    $data[] = [
                        "id" => (int)$val["lid"],
                        "name" => $val["name"],
                        "num" => (int)$GoodsCount,
                        "time" => [$val["start_time"]*1000, $val["end_time"]*1000],
                    ];
                }
            }

            $array = [
                "list" => $data,
                "total" => $count
            ];
            Code(200, false, $array);
        }else{
            Code(403, "服务器错误，请稍后再试");
        }
    }

}